package com.qdlc.p2p.manage.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.BorrowRepaymentService;
import com.qdlc.p2p.biz.util.excel.ExcelUtils;
import com.qdlc.p2p.biz.util.excel.JsGridReportBase;
import com.qdlc.p2p.biz.util.excel.TableData;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.constant.BorrowRepaymentStatus;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.model.BorrowRepaymentModel;

/**
 * 借贷管理-待还信息管理
 * 
 * @author xx
 * @version 2.0
 * @since 2014年4月17日
 */
public class ManageRepaymentAction extends BaseAction<BorrowRepaymentModel> implements ModelDriven<BorrowRepaymentModel> {

	@Resource
	private BorrowRepaymentService borrowRepaymentService;

	private Map<String, Object> data = new HashMap<String, Object>();

	/**
	 * 待还信息展示
	 * 
	 * @return
	 */
	@Action("/modules/loan/repayment/repaymentManager")
	public String repaymentManager() throws Exception {
		double remainderAccount  = borrowRepaymentService.getRemainderAccount();
		request.setAttribute("remainderAccount",remainderAccount);

		return "repaymentManager";
	}

	/**
	 * 还款管理列表
	 * 
	 * @return
	 */
	@Action("/modules/loan/repayment/repaymentList")
	public void repaymentList() throws Exception {
		model.setSize(paramInt("rows"));
		PageDataList<BorrowRepaymentModel> list = borrowRepaymentService.list(model, searchName);
		data.put("total", list.getPage().getTotal());
		data.put("rows", list.getList());
		printJson(getStringOfJpaObj(data));
	}
	/**
	 * 导出报表
	 * 
	 * @throws Exception
	 */
	@Action("/modules/loan/repayment/exportExcelRepayment")
	public void exportExcelRepayment() throws Exception {
		response.setContentType("application/msexcel;charset=UTF-8");
		// 最多出5000条记录
		model.setSize(5000);
		List<BorrowRepaymentModel> list = borrowRepaymentService.list(model, searchName).getList();
		String title = "还款管理Excel表";
		String[] hearders = new String[] { "编号", "用户名", "借款人姓名", "标名", "期数", "预计还款时间", "实际还款时间", "预还金额", "已还金额", "本金", "利息", "状态" };// 表头数组
		String[] fields = new String[] { "id", "userName", "realName", "borrowName", "actualPeriod", "repaymentTime",
				"repaymentYesTime", "repaymentAccount", "repaymentYesAccount", "capital", "interest", "statusString" };// 对象属性数组
		TableData td = ExcelUtils.createTableData(list, ExcelUtils.createTableHeader(hearders), fields);
		JsGridReportBase report = new JsGridReportBase(request, response);
		report.exportToExcel(title, getOperator().getUserName(), td);
	}
	/**
	 * 垫付信息展示
	 * 
	 * @return
	 */
	@Action("/modules/loan/repayment/advanceManager")
	public String advanceManager() throws Exception {

		return "advanceManager";
	}

	/**
	 * 垫付信息列表
	 * 
	 * @return
	 */
	@Action("/modules/loan/repayment/advanceList")
	public void advanceList() throws Exception {
		model.setSize(paramInt("rows"));
		model.setStatus(BorrowRepaymentStatus.STATUS_INSTEAD_REPAY); // 网站垫付
		PageDataList<BorrowRepaymentModel> list = borrowRepaymentService.list(model, searchName);
		data.put("total", list.getPage().getTotal());
		data.put("rows", list.getList());
		printJson(getStringOfJpaObj(data));
	}
}
